package com.ghsc.ghxc.jsb.ui.integral

import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.cn.appcore.adapter.CustomFragmentStateAdapter
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.ext.getCompatColor
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.ext.showToast
import com.cn.appcore.http.constant.HttpConstant
import com.cn.appcore.utils.Preference
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.LoginHelper
import com.ghsc.ghxc.jsb.databinding.ActivityIntegralMainBinding
import com.ghsc.ghxc.jsb.ui.integral.fragment.IntegralSupermarketHomeFragment
import com.ghsc.ghxc.jsb.ui.integral.fragment.IntegralSupermarketMyFragment
/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.integral
 * 类名：IntegralSupermarketActivity
 * 创建时间：2024/5/20 10:39
 * 创建人 @author： xiaolin
 * 描述 ：Todo...积分超市
 */
class IntegralSupermarketActivity : BaseVMActivity<IntegralSupermarketViewModel, ActivityIntegralMainBinding>()  {
    override fun attachLayoutRes(): Int {
        return R.layout.activity_integral_main
    }

    override fun attachVMClass(): Class<IntegralSupermarketViewModel>? {
        return IntegralSupermarketViewModel::class.java
    }


    private var mFragmentAdapter: CustomFragmentStateAdapter<Fragment>? = null
    private var selectIndex = 0
    var userRid: String by Preference(HttpConstant.USER_RID, "")
    var userId: String by Preference(HttpConstant.USER_ID, "")

    override fun onResume() {
        super.onResume()
    }
    override fun initBar() {
        super.initBar()
        mImmersionBar!!.statusBarDarkFont(true).init()
    }
    override fun initView() {



        binding.bar.topTitle.text="积分超市"
        binding.bar.topLeftView.setSingleClickListener { finish() }



        var listFragment = arrayListOf<Fragment>()
        listFragment.add(IntegralSupermarketHomeFragment())
        listFragment.add(IntegralSupermarketMyFragment())
        mFragmentAdapter = CustomFragmentStateAdapter<Fragment>(this).apply {
            setNewData(listFragment)
        }
        binding.myViewPage.adapter = mFragmentAdapter
        binding.myViewPage.offscreenPageLimit =
            mFragmentAdapter?.itemCount ?: ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT
        binding.myViewPage.isUserInputEnabled = false
    }

    override fun initData() {
        super.initData()
        setSelect(0)
    }
    private fun setSelect(it: Int) {
        selectIndex = it
        //这里判断是确保未被拦截
        if (selectIndex == it) {
            showFragment(it)
            tabInitView()
            when (it) {
                0 -> {
                    binding.imgPage.setImageResource(R.drawable.ic_main_home_selected)
                    binding.tvPage.setTextColor(getCompatColor(R.color.main_color))

                }
                1 -> {
                    binding.imgMy.setImageResource(R.drawable.ic_main_mine_selected)
                    binding.tvMy.setTextColor(getCompatColor(R.color.main_color))
                }
                else -> {}
            }

        }
    }
    //切换
    private fun showFragment(index: Int) {
        binding.myViewPage.setCurrentItem(index, false)
    }

    //默认状态
    private  fun tabInitView(){
        binding.tvPage.text = "首页"
        binding.tvMy.text = "我的"
        binding.imgPage.setImageResource(R.drawable.ic_main_home_unselected)
        binding.imgMy.setImageResource(R.drawable.ic_main_mine_unselected)

        binding.tvPage.setTextColor(getCompatColor(R.color.color_4d4d4d))
        binding.tvMy.setTextColor(getCompatColor(R.color.color_4d4d4d))
    }


    override fun initClickListener() {
        super.initClickListener()
        binding.run {
            llBtnPage.setSingleClickListener {
                setSelect(0)
            }
            llBtnMy.setSingleClickListener {
                setSelect(1)
            }
        }
    }

    override fun initRequestObserver() {
        super.initRequestObserver()
        mViewModel.apply {
            errorMsg.observe(this@IntegralSupermarketActivity) {
                showToast(it)
            }
            againLoginMsg.observe(this@IntegralSupermarketActivity) {
                showToast(CustKey.IN_AGAIN_MSG)
                LoginHelper.startLoginPage()
            }
        }
    }
}